package com.jiazhong.mybatis.m5;

import com.jiazhong.commons.bean.Emp;
import com.jiazhong.mybatis.m5.mapper.EmpMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class App {

    private static void a() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 第一次查询
        Emp emp1 = empMapper.findById(7900);
        // 第二次查询 用了第一次查询的结果（没有查询数据库） 一级缓存
        Emp emp2 = empMapper.findById(7900);
        // 第三次查询 查询条件不一样 就不能使用上一次的查询
        Emp emp3 = empMapper.findById(7897);
        System.out.println(emp1);
        System.out.println(emp2);
        System.out.println(emp3);
        sqlSession.close();
    }

    // 一级缓存起作用了
    private static void b() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper1 = sqlSession.getMapper(EmpMapper.class);
        Emp emp1 = empMapper1.findById(7900);
        System.out.println(emp1);
        EmpMapper empMapper2 = sqlSession.getMapper(EmpMapper.class);
        Emp emp2 = empMapper2.findById(7900);
        System.out.println(emp2);
        sqlSession.close();

    }


    // 一级缓存失效了，一级缓存生命周期：会话（session）
    private static void c() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        EmpMapper mapper1 = sqlSession1.getMapper(EmpMapper.class);
        Emp emp1 = mapper1.findById(7900);
        System.out.println(emp1);
        sqlSession1.close();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        EmpMapper mapper2 = sqlSession2.getMapper(EmpMapper.class);
        Emp emp2 = mapper2.findById(7900);
        System.out.println(emp2);
        sqlSession1.close();
    }
    // 演示一级缓存失效情况（c方法不在探讨）
    private static void d1() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 第一次查询
        Emp emp1 = empMapper.findById(7900);
        // 清空缓存
        sqlSession.clearCache();
        Emp emp2 = empMapper.findById(7900);
        System.out.println(emp1);
        System.out.println(emp2);
        sqlSession.close();
    }
    private static void d2() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 第一次查询
        Emp emp1 = empMapper.findById(7900);
        // 只要执行了更新操作(insert、update以及delete) 都会清空缓存
        int row = empMapper.delete(88888);
        System.out.println(row);
        Emp emp2 = empMapper.findById(7900);
        System.out.println(emp1);
        System.out.println(emp2);
        sqlSession.close();
    }
    public static void main(String[] args) {
        d2();
    }
}